import 'package:flutter/material.dart';

import '../common/const/app_color_const.dart';
import '../common/const/app_style_const.dart';
import '../common/type/status_tab_bar_type.dart';

class StatusTabBar extends StatefulWidget {
  final double? height;
  final EdgeInsets? padding;
  final String tableKey;
  final List<StatusTabBarType> tabList;
  final double activeLineWidth;
  final Color? activeLineColor;
  final Color? activeColor;
  final Color? activeBackground;
  final Color? background;
  final Color? color;
  final double space;
  final double borderRadius;
  final bool shrinkWrap;
  final EdgeInsets? labelPadding;
  final Widget Function(BuildContext context, StatusTabBarType statusTabBar,
      String activeTabKey, TextStyle style)? labelBuilder;
  final Function(StatusTabBarType tab, String activeTabKey) onActive;

  const StatusTabBar(
      {Key? key,
      required this.tabList,
      required this.onActive,
      this.height = 45,
      this.padding,
      this.labelPadding,
      this.borderRadius = 3,
      this.activeLineWidth = 1,
      this.activeColor = primaryColor,
      this.activeLineColor = primaryColor,
      this.activeBackground = primaryTransparentColor3,
      this.background = whiteColor,
      this.color = blackColor,
      this.shrinkWrap = false,
      this.tableKey = '',
      this.space = 4,
      this.labelBuilder})
      : super(key: key);

  @override
  State<StatusTabBar> createState() => StatusTabBarState();
}

class StatusTabBarState extends State<StatusTabBar> {
  GlobalKey tabBarGlobalKey = GlobalKey();
  String activeTabKey = '';
  ScrollController scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    activeTabKey = widget.tableKey;
    for (var element in widget.tabList) {
      element.globalKey ??= GlobalKey();
    }
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  void scrollControllerAnimateTo(dx) {
    scrollController.animateTo(scrollController.offset.toDouble() + dx,
        duration: const Duration(milliseconds: 300), curve: Curves.ease);
  }

  Widget buildTab(index) {
    var activeTextStyle = TextStyle(
      color: widget.tabList[index].key == activeTabKey
          ? widget.activeColor
          : widget.color,
    );
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Padding(
          padding: EdgeInsets.only(left: widget.space, right: widget.space),
          child: InkWell(
            key: widget.tabList[index].globalKey,
            onTap: () {
              activeTabKey = widget.tabList[index].key;
              widget.onActive(widget.tabList[index], activeTabKey);

              RenderBox? renderBox = widget
                  .tabList[index].globalKey?.currentContext!
                  .findRenderObject() as RenderBox;

              var dx =
                  renderBox.localToGlobal(Offset(0.0, renderBox.size.width)).dx;

              if (dx < 0) {
                scrollControllerAnimateTo(dx - widget.space);
              }

              double width = tabBarGlobalKey.currentContext!.size!.width;

              if ((dx + renderBox.size.width) > width) {
                scrollControllerAnimateTo(
                    (dx + renderBox.size.width + width - widget.space));
              }

              setState(() {});
            },
            child: Ink(
              padding: widget.labelPadding ??
                  const EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
              decoration: BoxDecoration(
                  color: widget.tabList[index].key == activeTabKey
                      ? widget.activeBackground
                      : widget.background,
                  borderRadius:
                      BorderRadius.all(Radius.circular(widget.borderRadius)),
                  border: Border.fromBorderSide(BorderSide(
                      color: widget.tabList[index].key == activeTabKey
                          ? widget.activeLineColor!
                          : widget.background!,
                      width: widget.activeLineWidth))),
              child: Center(
                child: null != widget.labelBuilder
                    ? widget.labelBuilder!(context, widget.tabList[index],
                        activeTabKey, activeTextStyle)
                    : Text(
                        widget.tabList[index].label,
                        style: activeTextStyle,
                      ),
              ),
            ),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Ink(
      key: tabBarGlobalKey,
      height: widget.height,
      padding: widget.padding ?? EdgeInsets.zero,
      decoration: BoxDecoration(
          color: widget.background,
          border: const Border(bottom: borderStyle)),
      child: ListView(
        shrinkWrap: widget.shrinkWrap,
        controller: scrollController,
        scrollDirection: Axis.horizontal,
        children:
            List.generate(widget.tabList.length, (index) => buildTab(index)),
      ),
    );
  }
}
